package com.xsb.customview.searchview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义流式布局（历史搜索记录）
 */
public class FlowLayout extends ViewGroup {
    private static final String TAG = "FlowLayout";

    /**
     * 用来保存每行views的列表
     */
    private List<List<View>> mViewLinesList = new ArrayList<>();
    /**
     * 用来保存行高的列表
     */
    private List<Integer> mLineHeights = new ArrayList<>();


    private int defaultShowCount; //默认显示个数 若为0则显示全部
    private int lineSpace; //行间距
    private int leftMargin; //左间距
    private int rightMargin; //右间距
    private float corner; //圆角 单位为dp
    private int flowTextColor; //字体颜色
    private int flowTextBgColor; //背景颜色
    private float flowTextSize; //字体大小 单位为sp
    private int paddingLeft;
    private int paddingRight;
    private int paddingTop;
    private int paddingBottom;
    //设置的数据源
    private List<String> strings;

    public FlowLayout(Context context) {
        super(context, null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttributeSet(context, attrs);
    }

    //获取自定义属性
    private void getAttributeSet(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout);
        lineSpace = (int) array.getDimension(R.styleable.FlowLayout_lineSpace, Utils.dp2px(getContext(), 10));
        corner = (int) array.getDimension(R.styleable.FlowLayout_flowTextCorner, Utils.dp2px(getContext(), 5));
        leftMargin = (int) array.getDimension(R.styleable.FlowLayout_flowTextLeftMargin, Utils.dp2px(getContext(), 10));
        rightMargin = (int) array.getDimension(R.styleable.FlowLayout_flowTextRightMargin, Utils.dp2px(getContext(), 10));
        flowTextColor = array.getColor(R.styleable.FlowLayout_flowTextColor, Color.parseColor("#ff0000"));
        flowTextBgColor = array.getColor(R.styleable.FlowLayout_flowTextBgColor, Color.parseColor("#cfcfcf"));
        flowTextSize = array.getDimension(R.styleable.FlowLayout_flowTextSize, Utils.sp2px(getContext(), 12));
        paddingLeft = (int) array.getDimension(R.styleable.FlowLayout_flowTextPaddingLeft, Utils.dp2px(getContext(), 10));
        paddingRight = (int) array.getDimension(R.styleable.FlowLayout_flowTextPaddingRight, Utils.dp2px(getContext(), 10));
        paddingTop = (int) array.getDimension(R.styleable.FlowLayout_flowTextPaddingTop, Utils.dp2px(getContext(), 5));
        paddingBottom = (int) array.getDimension(R.styleable.FlowLayout_flowTextPaddingBottom, Utils.dp2px(getContext(), 5));
        defaultShowCount = array.getInteger(R.styleable.FlowLayout_default_show_count, 0);
        array.recycle();
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        int measuredWith = 0; //flowLayout的最终测量宽度
        int measuredHeight = 0; //flowLayout的最终测量高度
        int currentLineWidth = 0; //当前行宽度
        int currentLineHeight = 0; //当前行高度

        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
            Log.d(TAG, "宽高确定");
            measuredWith = widthSpecSize;
            measuredHeight = heightSpecSize;
        } else {
            Log.d(TAG, "宽高不确定，重新测量");
            mViewLinesList.clear();
            mLineHeights.clear();
            int childWidth;//子View的宽度
            int childHeight;//子view的高度
            int childCount = getChildCount();//flowLayout的子View的个数
            List<View> viewList = new ArrayList<>();//存放子view的集合
            for (int i = 0; i < childCount; i++) {
                View childView = getChildAt(i);
                measureChild(childView, widthMeasureSpec, heightMeasureSpec);
                MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams();
                childWidth = childView.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
                childHeight = childView.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;

                if (currentLineWidth + childWidth > widthSpecSize) {//需要换行
                    /*记录当前行的信息*/
                    //刷新flowLayout的最终测量宽度 和 flowLayout的最终测量高度
                    measuredWith = Math.max(measuredWith, currentLineWidth);
                    measuredHeight = (int) (measuredHeight + currentLineHeight + lineSpace);
                    //将当前行的viewList添加到总的List,将行高添加至总行的行高list
                    mViewLinesList.add(viewList);
                    mLineHeights.add(currentLineHeight);

                    /*记录新一行的信息*/
                    //重新赋值新一行的宽，高
                    currentLineWidth = childWidth;
                    currentLineHeight = childHeight;

                    //新建一行的viewList，添加新一行的view
                    viewList = new ArrayList<>();
                    viewList.add(childView);
                } else {//不需要换行
                    /*记录行内的消息*/
                    //行内宽度的叠加，高度的比较
                    currentLineWidth += childWidth;
                    currentLineHeight = Math.max(currentLineHeight, childHeight);
                    //添加至当前行的viewList中
                    viewList.add(childView);
                }

                if (i == childCount - 1) {//如果是最后一个view，不管需不需要换行，都要把信息存储到map集合里
                    //记录当前行的最大宽度，高度累加
                    measuredWith = Math.max(measuredWith, currentLineWidth);
                    measuredHeight = measuredHeight + currentLineHeight + lineSpace;

                    //记录当前行添加至总的list中
                    mViewLinesList.add(viewList);
                    mLineHeights.add(currentLineHeight);
                }
            }
            if (getPaddingLeft() != 0 || getPaddingRight() != 0 || getPaddingTop() != 0 || getPaddingBottom() != 0) {
                measuredWith = measuredWith + getPaddingLeft() + getPaddingRight();
                measuredHeight = measuredHeight + getPaddingTop() + getPaddingBottom();
            }
        }
        setMeasuredDimension(measuredWith, measuredHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left, top, right, bottom;
        int curTop = getPaddingTop();
        int curLeft = getPaddingLeft();
        for (List<View> viewList : mViewLinesList) {
            for (View childView : viewList) {
                MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams();
                left = curLeft + layoutParams.leftMargin;
                top = curTop + layoutParams.topMargin;
                right = left + childView.getMeasuredWidth();
                bottom = curTop + childView.getMeasuredHeight();
                childView.layout(left, top, right, bottom);
                curLeft += childView.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
            }
            //重置左边距
            curLeft = getPaddingLeft();
            curTop += mLineHeights.get(mViewLinesList.indexOf(viewList)) + lineSpace;
        }
        mViewLinesList.clear();
        mLineHeights.clear();
    }

    @Override
    public void invalidate() {
        super.invalidate();
    }

    //设置数据
    public void setData(List<String> data){
        this.strings = data;
        if(strings != null && !strings.isEmpty()){
            int maxcount;
            if(defaultShowCount > 0){
                maxcount = Math.min(defaultShowCount,strings.size());
            }else{
                maxcount = strings.size();
            }
            for (int i = 0; i < maxcount; i++) {
                setNormalFlowText(i);
            }
            if (defaultShowCount > 0 && defaultShowCount < strings.size()) {
                setArrowFlowText(maxcount);
            }
        }
    }

    /**
     * 设置为箭头的flowtext 点击可以展开
     *
     * @param maxCount flowtext最大个数
     */
    private void setArrowFlowText(final int maxCount) {
        FlowTextView flowTextView = new FlowTextView(getContext());
        SpannableString arrowSpan = new SpannableString("h");
        Drawable drawable = getResources().getDrawable(R.mipmap.arrow_32x32);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), (int) flowTextSize);
        ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
        arrowSpan.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        flowTextView.setTag("arrow");
        flowTextView.setText(arrowSpan);
        setFlowText(maxCount, flowTextView);
    }

    //设置普通flowText
    private void setNormalFlowText(int i) {
        FlowTextView flowTextView = new FlowTextView(getContext());
        flowTextView.setText(strings.get(i));
        flowTextView.setTextColor(flowTextColor);
        flowTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,flowTextSize);
        setFlowText(i,flowTextView);
    }

    //设置通用属性
    private void setFlowText(int index, FlowTextView flowTextView) {
        flowTextView.setPadding(paddingLeft,paddingTop,paddingRight,paddingBottom);
        flowTextView.setCorner((int)corner);
        flowTextView.setBackgroundColor(flowTextBgColor);
        flowTextView.setIncludeFontPadding(false);
        flowTextView.setGravity(Gravity.CENTER);
        flowTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(v,index);
                    if("arrow".equals(v.getTag())){
                        //点击下箭头(展示全部)
                        showAllFlowTextViews(index,v);
                    }
                }
            }
        });
        MarginLayoutParams marginLayoutParams = new MarginLayoutParams(MarginLayoutParams.WRAP_CONTENT,MarginLayoutParams.WRAP_CONTENT);
        marginLayoutParams.setMargins(leftMargin,0,rightMargin,0);
        addView(flowTextView,marginLayoutParams);
    }


    /**
     * 显示全部flowtextview
     *
     * @param index 展示剩余flowtextview的起始索引
     * @param v     flowtextview
     */
    private void showAllFlowTextViews(int index, View v) {
        removeView(v);
        if (index < strings.size() && index > 0) {
            for (int i = index; i < strings.size(); i++) {
                setNormalFlowText(i);
            }
        }
    }


    //设置接口，用于传递事件
    private OnItemClickListener onItemClickListener;
    public interface OnItemClickListener {
        void onItemClick(View v, int index);
    }

    public void setOnItemClickListener(final OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }
}
